package com.example.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 时间格式化处理
 *
 * @author: NPF
 * @date: 2018/4/18.
 */
public class LKTimeUtil {


    private static LKTimeUtil mInstance;

    private LKTimeUtil() {
    }

    public static LKTimeUtil getInstance() {
        if (null == mInstance) {
            synchronized (LKTimeUtil.class) {
                if (null == mInstance) {
                    mInstance = new LKTimeUtil();
                }
            }
        }
        return mInstance;
    }

    /**
     * 格式化时间
     * <p>
     * 一分钟内：刚刚
     * 一小时内：xx分钟前
     * 一天内：HH:mm
     * 一年内：MM-dd HH:mm
     * 一年外：yyyy-MM-dd HH:mm
     *
     * @param formatTimeMillis
     */
    public String formatTimeA(long formatTimeMillis) {
        String formatTimeStr;
        long currentTimeMillis = System.currentTimeMillis();
        if (isSameMinute(currentTimeMillis, formatTimeMillis)) {//一分钟内
            formatTimeStr = "刚刚";
        } else if (isSameHour(currentTimeMillis, formatTimeMillis)) {//一小时内处理
            long millisDifference = Math.abs(currentTimeMillis - formatTimeMillis) / 60000;
            if (millisDifference > 1) {
                formatTimeStr = String.format(Locale.CHINESE, "%d分钟前", millisDifference);
            } else {
                formatTimeStr = "刚刚";
            }
        } else if (isSameDay(currentTimeMillis, formatTimeMillis)) {//同一天处理
            formatTimeStr = formatTime("HH:mm", formatTimeMillis);
        } else if (isSameYear(currentTimeMillis, formatTimeMillis)) {//同一年处理
            formatTimeStr = formatTime("MM-dd HH:mm", formatTimeMillis);
        } else {
            formatTimeStr = formatTime("yyyy-MM-dd HH:mm", formatTimeMillis);
        }
        if (formatTimeStr.startsWith("1970")) {
            formatTimeStr = "";
        }
        return formatTimeStr;
    }

    /**
     * 今天:今天
     * 今天以前:yyyy-MM-dd
     *
     * @param formatTimeMillis
     * @return
     */
    public String formatTimeB(long formatTimeMillis) {
        String formatTimeStr;
        long currentTimeMillis = System.currentTimeMillis();
        if (isSameDay(currentTimeMillis, formatTimeMillis)) {
            formatTimeStr = "今天";
        } else {
            formatTimeStr = formatTime("yyyy.MM.dd", formatTimeMillis);
        }
        if (formatTimeStr.startsWith("1970")) {
            formatTimeStr = "";
        }
        return formatTimeStr;
    }

    /**
     * 今天:今天
     * 今年以前:yyyy年MM月dd日
     * 今年:MM月dd日
     *
     * @param formatTimeMillis
     * @return
     */
    public String formatTimeC(long formatTimeMillis) {
        String formatTimeStr;
        long currentTimeMillis = System.currentTimeMillis();
        if (isSameDay(currentTimeMillis, formatTimeMillis)) {
            formatTimeStr = "今天";
        } else if (isSameYear(currentTimeMillis, currentTimeMillis)) {
            formatTimeStr = formatTime("MM月dd日", formatTimeMillis);
        } else {
            formatTimeStr = formatTime("yyyy年MM月dd日", formatTimeMillis);
        }
        if (formatTimeStr.startsWith("1970")) {
            formatTimeStr = "";
        }
        return formatTimeStr;
    }


    /**
     * yyyy/MM/dd HH:mm
     *
     * @param formatTimeMillis
     * @return
     */
    public String formatTimeD(long formatTimeMillis) {
        String formatTimeStr;
        formatTimeStr = formatTime("yyyy/MM/dd HH:mm", formatTimeMillis);
        if (formatTimeStr.startsWith("1970")) {
            formatTimeStr = "";
        }
        return formatTimeStr;
    }

    /**
     * 格式化时间
     * <p>
     * 一天内：HH:mm
     * 一年内：MM月dd日
     * 一年外：yyyy年
     *
     * @param formatTimeMillis
     */
    public String formatTimeE(long formatTimeMillis) {
        String formatTimeStr;
        long currentTimeMillis = System.currentTimeMillis();
        if (isSameDay(currentTimeMillis, formatTimeMillis)) {//同一天处理
            formatTimeStr = formatTime("HH:mm", formatTimeMillis);
        } else if (isSameYear(currentTimeMillis, formatTimeMillis)) {//同一年处理
            formatTimeStr = formatTime("MM月dd日", formatTimeMillis);
        } else {
            formatTimeStr = formatTime("yyyy年", formatTimeMillis);
        }
        if (formatTimeStr.startsWith("1970")) {
            formatTimeStr = "";
        }
        return formatTimeStr;
    }


    /**
     * 判断两个时间是否为同一分钟
     *
     * @param date1
     * @param date2
     * @return
     */
    public boolean isSameMinute(long date1, long date2) {
        return Math.abs(date1 - date2) / 60000 < 2;
    }

    /**
     * 判断两个时间是否为同一小时
     *
     * @param date1
     * @param date2
     * @return
     */
    public boolean isSameHour(long date1, long date2) {
        return Math.abs(date1 - date2) / 60000 < 60;
    }

    /**
     * 判断两个时间是否为同一天
     *
     * @param date1
     * @param date2
     * @return
     */
    public boolean isSameDay(long date1, long date2) {
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTimeInMillis(date1);
        Calendar calDateB = Calendar.getInstance();
        calDateB.setTimeInMillis(date2);
        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
                && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
                && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB
                .get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 判断两个时间是否为同一年
     *
     * @param date1
     * @param date2
     * @return
     */
    public boolean isSameYear(long date1, long date2) {
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTimeInMillis(date1);
        Calendar calDateB = Calendar.getInstance();
        calDateB.setTimeInMillis(date2);
        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR);
    }

    /**
     * 判断两个时间是否为同一个月
     *
     * @param date1
     * @param date2
     * @return
     */
    public boolean isSameMonth(long date1, long date2) {
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTimeInMillis(date1);
        Calendar calDateB = Calendar.getInstance();
        calDateB.setTimeInMillis(date2);
        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
                && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
                && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB
                .get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 格式化时间
     *
     * @param model
     * @param formatTimeMillis
     * @return
     */
    public String formatTime(String model, long formatTimeMillis) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(model);
        return dateFormat.format(new Date(formatTimeMillis));
    }

    /**
     * 获取两个时间之间的间隔,分钟
     *
     * @param time1
     * @param time2
     * @return
     */
    public int getTimeInterval(long time1, long time2) {
        long longIntervalTime = Math.abs(time1 - time2);
        return (int) (longIntervalTime / 60000);//间隔的分钟
    }

}
